package com.yourname.custom;

import com.yourname.entity.Student;

import java.util.ArrayList;

public class CustomStudentList extends ArrayList<Student> {
    @Override
    public boolean contains(Object o) {
        Student s=(Student) o;
        for (Student student:this){
            if (student.getId().equals(s.getId())){
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean add(Student student) {
        if (contains(student)){
            System.out.printf("学生%s已存在",student.getId());
            return false;
        }else{
            return super.add(student);
        }
    }
    public Student get(String sid) {
        for (Student student:this){
            if (student.getId().equals(sid)){
                return student;
            }
        }
        return null;
    }
    public boolean update(String sid,Student newStudent) {
        for (int i=0;i<this.size();i++){
            if (this.get(i).getId().equals(sid)){
                this.set(i,newStudent);
                return true;
            }
        }
        return false;
    }

    public Student set(Student newStudent){
        Student ori=null;
        for (int i=0;i<this.size();i++){
            Student target=this.get(i);
            ori=target;
            if (this.get(i).getId().equals(newStudent.getId())){
                return this.set(i,newStudent);
            }
        }
        return ori;
    }
}
